package com.zhihuishu.treenity.util;

import java.lang.reflect.Method;
/**
 * @author zhouzha 克隆对象
 */
public class CopyObjUtils {
	public static void copyObj(Object class1, Object class2) {  
	       try {  
	           Class<?> clazz1 = class1.getClass();  
	           Class<?> clazz2 = class2.getClass();  
	           Method[] method1 = clazz1.getMethods();  
	           Method[] method2 = clazz2.getMethods();  
	  
	           int length1 = method1.length;  
	           int length2 = method2.length;  
	           if (length1 != 0 && length2 != 0) {  
	               Method[] get = new Method[length2];  
	               for (int i = 0, j = 0; i < length2; i++) {  
	                   if (method2[i].getName().indexOf("get") == 0) {  
	                       get[j] = method2[i];  
	                       ++j;  
	                   }  
	               }  
	  
	               for (int i = 0; i < get.length; i++) {  
	                   if (get[i] == null)
	                       continue;  
	                   Object value = get[i].invoke(class2, new Object[] {});  
	                   if (null == value)  
	                       continue;  
	                   String getName = get[i].getName();  
	                   Class<?> paramType = get[i].getReturnType();  
	                   Method getMethod = null;  
	                   try {  
	                       getMethod = clazz1.getMethod(getName, new Class[] {});  
	                   } catch (NoSuchMethodException e) {  
	                       continue;  
	                   }  
	                   if (null == getMethod || null != getMethod.invoke(class1, new Object[] {}))  
	                       continue;  
	                   String setName = "set" + getName.substring(3);  
	                   Method setMethod = clazz1.getMethod(setName, paramType);  
	                   setMethod.invoke(class1, value);  
	               }  
	           }  
	       } catch(Exception e) {  
	    	   
	       }  
	   }  
}
